/**
 * 
 */
package any.tag.gwt.client;

import any.gwt.client.http.AsynClient;
import any.gwt.client.http.AsynClientListener;
import any.gwt.client.http.AsynClient.PreSendProcessor;
import any.gwt.client.util.QueryBuilder;

import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.URL;

/**
 * TODO: Doc me
 * 
 * @author keke <keke@codehaus.org>
 * @version
 * @revision $Revision:$
 */
public class TagClient {
    private final AsynClient asynClient = new AsynClient();

    public void addClientListener(final AsynClientListener listener) {
        asynClient.addClientListener(listener);
    }

    public void getAllTags(final AsynClientListener listener) {
        asynClient.get("alltags", "/any/tag/api/tags.json", null,
                new PreSendProcessor() {
                    public void process(final RequestBuilder requestBuilder) {
                        requestBuilder.setHeader("Accept",
                                "application/json");
                    }
                }, listener);
    }

    public void getItemsByTag(final String tagName,
            final AsynClientListener listener) {
        asynClient.get("tags", "/any/tag/api/tag/"
                + URL.encodeComponent(tagName) + ".json", null,
                new PreSendProcessor() {
                    public void process(final RequestBuilder requestBuilder) {
                        requestBuilder.setHeader("Accept",
                                "application/json");
                    }
                }, listener);
    }

    public void getTagsOfURI(final String uri, final AsynClientListener listener) {
        asynClient.get("tagsOfURI", "/any/tag/api/tag/about/"
                + URL.encodeComponent(uri) + ".json", null,
                new PreSendProcessor() {
                    public void process(final RequestBuilder requestBuilder) {
                        requestBuilder.setHeader("Accept", "application/json");
                    }
                }, listener);
    }

    public void removeClientListener(final AsynClientListener listener) {
        asynClient.removeClientListener(listener);
    }

    public void tag(final String tags, final String aboutURI,
            final AsynClientListener listener) {
        asynClient.post("tag", "/any/tag/api/tag", new QueryBuilder().add(
                "tags", tags).add("about", aboutURI).build(),
                new PreSendProcessor() {

                    public void process(final RequestBuilder requestBuilder) {
                        requestBuilder.setHeader("content-type",
                                "application/x-www-form-urlencoded");
                        requestBuilder.setHeader("Accept", "application/json");
                    }
                }, listener);
    }
}
